#include <sys/shm.h>
#include <sys/ipc.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include "global.h"

struct all_info *shm_init(void)
{
	key_t key;
	int shmid;
	struct all_info *shm_buf;
	if((key = ftok("/app", 's')) < 0)
	{
		perror("fail to ftok");
		exit(1);
	}
	if((shmid = shmget(key, sizeof(struct all_info), IPC_CREAT|IPC_EXCL|0666)) == -1)
	{
		if(errno == EEXIST)
		{
			shmid = shmget(key, 0, 0);
		}
		else
		{
			perror("fail to shmget");
			exit(1);
		}
	}
	if((shm_buf = (struct all_info *)shmat(shmid, NULL, 0)) == (void *)-1)
	{
		perror("fail to shmat");
		exit(1);
	}
	printf("****************shm is ok***********************\n");
	return shm_buf;
}
//测试程序
/*
int main(int argc, const char *argv[])
{
	int temp = 0;
	pid_t pid = fork();
	if(pid == 0)
	{
		struct all_info *shm_buf;
		shm_buf = shm_init();
		while(1)
		{
			temp ++;
			shm_buf->storage_dev[0].s_env.e_temperature = temp;
			sleep(1);
		}
	}
	else if(pid > 0)
	{
		struct all_info *shm_buf1;	
		shm_buf1 = shm_init();
		while(1)
		{
			sleep(1);
			printf("temp = %d\n", shm_buf1->storage_dev[0].s_env.e_temperature);
		}
	}	
	return 0;
}
*/
